package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Transactional
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    ApplicationMapper applicationMapper;

    @Autowired
    AuditService auditService;

    @Autowired(required = false)
    AuditMapper auditMapper;

    @Autowired
    UserMapper userMapper;


    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        Application application = new Application();
        //将前端传过来的数据赋值给application对象
        BeanUtils.copyProperties(applicationSaveParam,application);
        //新增的申请表，初始状态为已发起
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        //由于业务原因，没有修改，可以实现撤销，然后重新发申请单
        applicationMapper.insert(application);
        //还需要调用审批业务层，为当前申请单同步新增对应的审批数据
        auditService.insertAudit(application);

    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单列表数据，applicationQuery={}",applicationQuery);
        //根据查询条件，查询符合的申请单数据
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历申请单集合，依次查出每个申请单对应的审批人数据
        for(int i = 0;i<list.size();i++){
            ApplicationVO applicationVO = list.get(i);
            //申请单列表中需要审批人的相关信息，我们可以自定义方法给applicationVO设置相关的审批人数据
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    private void  assignAuditUserList(ApplicationVO applicationVO){
        //1.准备空集合，分别用来保存当前申请单对应的审批人姓名集合与审批人id集合
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //2.根据传入的申请单，查出对应的审批单数据，注意：一个申请单可能对应多个审批单!
        List<AuditVO> auditVOList = auditMapper.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历出每一个审批单对象
        for(int i = 0;i<auditVOList.size();i++){
            //4.取出当前遍历到的审批单对象
            AuditVO auditVO = auditVOList.get(i);
            //5.取出当前审批单对应的审批人id并装入上方的空集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //6.根据审批单的审批人id，到用户数据层查出对应审批人的用户数据
            userMapper.selectById(id);
        }
    }
}
